package com.minyifang.wcspf.work.ui.home.fragments.home2sub;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.minyifang.wcspf.R;
import com.minyifang.wcspf.common.adapter.GeneralFragmentPagerAdapter;
import com.minyifang.wcspf.common.base.BaseFragment;
import com.minyifang.wcspf.common.base.BaseLazyLoadFragment;
import com.minyifang.wcspf.common.utils.ArrayUtil;
import com.minyifang.wcspf.work.ui.home.model.ClassBean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;

/**
 * 二级分类页面
 * create an instance of this fragment.
 */
public class SubClassFragment extends BaseLazyLoadFragment {

    List<ClassBean.SubClassBean> mSubClassBeanList = new ArrayList<>();
    List<String> titles = new ArrayList<>();
    List<Fragment> mFragments = new ArrayList<>();
    GeneralFragmentPagerAdapter mGeneralFragmentPagerAdapter;


    @BindView(R.id.vp_class)
    ViewPager mViewPager;


    public SubClassFragment() {
    }

    public static SubClassFragment newInstance(Serializable data) {
        Bundle args = new Bundle();
        args.putSerializable("data", data);
        SubClassFragment fragment = new SubClassFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            Serializable serializable = getArguments().getSerializable("data");
            if (serializable != null) {
                mSubClassBeanList = ((ClassBean) serializable).subClassBeans;
            }
        }
    }

    private void updateDisplay() {
        titles.clear();
        titles.addAll(ArrayUtil.listConvertToStringList(mSubClassBeanList, subClassBean -> subClassBean.name));
        mFragments.clear();
        for (int i = 0; i < mSubClassBeanList.size(); i++) {
            ClassBean.SubClassBean bean = mSubClassBeanList.get(i);
//            mFragments.add(GoodsListFragment.newInstance(bean));
        }
        mGeneralFragmentPagerAdapter = new GeneralFragmentPagerAdapter(getChildFragmentManager(), mFragments, titles);
        mViewPager.setAdapter(mGeneralFragmentPagerAdapter);
//        mTabLayout.setupWithViewPager(mViewPager);
        mViewPager.setOffscreenPageLimit(titles.size() - 1);
    }

    @Override
    protected int setLayoutResource() {
        return R.layout.fragment_sub_class;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    protected void lazyLoadData() {
        updateDisplay();
    }

    public void selectedSubClass(int i) {
        if (mFragments.size() > i)
            mViewPager.setCurrentItem(i);
    }
}
